const path = require('path')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin')
const ZipPlugin = require('zip-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
module.exports = env => {
  return {
    plugins: [
      new BundleAnalyzerPlugin({
        analyzerMode: 'static'
      }),
      new CompressionPlugin(),
      new ZipPlugin({
        filename: `${env.entry}.zip`,
        path: path.resolve(__dirname, `../dist`)
      }),
      new CleanWebpackPlugin(path.resolve(__dirname, `../dist`), {
        root: path.resolve(__dirname, '../'),
        verbose: true,
        dry: false
      }),
      new HtmlWebpackIncludeAssetsPlugin({
        assets: [
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/vuex/3.0.1/vuex.min.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.1/vue-router.min.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/element-ui/2.4.0/index.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0.rc2/echarts.min.js',
            type: 'js'
          },
          {
            path: '//cdn.jsdelivr.net/npm/v-charts/lib/line.min.js',
            type: 'js'
          },
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.23.0/polyfill.min.js',
            type: 'js'
          }
        ],
        append: false,
        publicPath: ''
      })

    ],
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          cache: true,
          parallel: true,
          sourceMap: true
        }),
        new OptimizeCSSAssetsPlugin({})
      ]
    },
    externals: {
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'axios': 'axios',
      'vuex': 'Vuex',
      'element-ui': 'ELEMENT',
      'echarts': 'echarts',
      'v-charts/lib/line': 'VeLine'
    }
  }
}
